using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TemplateWeb.Models.Repositories
{
	#region using
	using TemplateWeb.Helpers;
	using TemplateWeb.Models.ViewModels;
	#endregion
	public class LienKetRepository
	{
		public List<LienKet> SelectAll()
		{
			var lstItem = new List<LienKet>();
			var lst = DataContext.Instance.LienKets.ToList();
			for (int i = 0; i < lst.Count; i++)
			{
				lstItem.Add(lst[i]);
			}
			return lstItem;
		}

		public LienKet SelectByID(int id)
		{
			return DataContext.Instance.LienKets.FirstOrDefault(item => item.ID == id );
		}

		public List<LienKet> SelectPaging(int page, int pageSize)
		{
			var lst = DataContext.Instance.LienKets.ToList();
			return lst;
		}

		public bool Insert(LienKet obj)
		{
			try
			{
				DataContext.Instance.LienKets.InsertOnSubmit(obj);
				DataContext.Instance.SubmitChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public bool Delete(int id)
		{
			try
			{
				var delitem = DataContext.Instance.LienKets.FirstOrDefault(item => item.ID == id );
				DataContext.Instance.LienKets.DeleteOnSubmit(delitem);
				DataContext.Instance.SubmitChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public bool Save()
		{
			try
			{
				DataContext.Instance.SubmitChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public int GetTotalItem()
		{
			return DataContext.Instance.LienKets.Count();
		}


		public int GetTotalPage()
		{
			int RecordCount = GetTotalItem();
			int PageSize = WebConfiguration.ProductsPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}


		public List<LienKet> RetrieveByID(int id)
		{
			var lst = (from item in DataContext.Instance.LienKets where  item.ID == id  select item).ToList();
			var lstItem = new List<LienKet>();
			lstItem.Add(lst[0]);
			return lstItem;
		}

	}
}